<?php

namespace App\Http\Controllers\Stock\Manage;

use App\Http\Controllers\ShopController;
use App\Models\Business\Business as BusinessModel;
use App\Models\Admin\Admin as AdminModel;
use App\Models\Business\Source as SourceModel;
use Illuminate\Support\Facades\Validator;

class PrivateseaController extends ShopController
{
    public function index()
    {
        $adminid = request('adminid',0);
        
        $data = BusinessModel::where('adminid',$adminid)->get();

        return $this->success('查询成功',$data);
    }

    public function detail()
    {
        $id = request('id',0);
        
        $result = BusinessModel::with(['source'])->where('id',$id)->first();

        if($result)
        {
            return $this->success('获取成功',$result);
        }else{
            return $this->error('获取失败');
        }
    }

    public function edit()
    {
        // $id = request('id',0);
        $params = request()->input();
        $password = $params['password'] ?? '';
        if(!empty($password))
        {
            
            // dd($password);
            $salt = build_randstr();

            $params['password'] = md5($password . $salt);
            $params['salt'] = $salt;
        }
        
        $result = BusinessModel::where('id',$params['id'])->update($params);

        if($result !== false){
            return $this->success('修改成功');
        }else{
            return $this->error('修改失败');
        }
        // $result = BusinessModel::with(['source'])->where('id',$id)->first();
    }

    public function del()
    {

        $id = request('id',0);
        $result = BusinessModel::destroy($id);

        if($result === false)
        {
            return $this->error('删除失败');
        }else{
            return $this->success('删除成功');
        }
    }

    public function admin()
    {
        $result = AdminModel::get();

        return $this->success('查询成功',$result);
    }

    public function recovery()
    {
        $id = request('id',0);
        // $admin = request('admin',0);

        $result = BusinessModel::where('id',$id)->update(['adminid' => null]);

        if($result === false)
        {
            return $this->error('回收失败');
        }else{
            return $this->success('回收成功');
        }
    }

}